package com.shaoyu.spring.study.validate.core;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Objects;

/**
 * @Desc :
 * @Author: shaoyu
 * @Date : 2022/8/23 09:14
 */
@Aspect
public class ValidateAspect {

    @Pointcut("@annotation(com.shaoyu.spring.study.validate.annotation.ValidateInterceptMethod) && !@annotation(com.shaoyu.spring.study.validate.annotation.ValidateFor)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceedReturn;
        try {
            Object interceptResult = ValidateInterceptorExecutor.intercept(proceedingJoinPoint);
            if (Objects.nonNull(interceptResult)) {
                return interceptResult;
            }
            proceedReturn = proceedingJoinPoint.proceed();
        } finally {
            ValidateContext.remove();
        }
        return proceedReturn;
    }
}
